package com.etone.smartAudit.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.config.EGCCConfig;
import com.etone.smartAudit.domain.EgccSubribe;
import com.etone.smartAudit.dto.egcc.SubscriptDTO;
import com.etone.smartAudit.dto.egcc.SubscriptVO;
import com.etone.smartAudit.error.CustomParameterizedException;
import com.etone.smartAudit.mapper.EgccSubribeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author jiayen
 * @since 2022-03-30 14:14:48
 */
@Service
@Slf4j
public class EgccSubribeService extends ServiceImpl<EgccSubribeMapper, EgccSubribe> {

    @Autowired
    private EGCCConfig egccConfig;

    /***
     * 订阅
     * 提供应用的订阅功能，用于记录用户与应用的订阅关系。
     * @param   dto
     * @return
     */
    public SubscriptVO subscribe(SubscriptDTO dto) {
        validateParam(dto);
        ///app/subscribe
        String uid = dto.getUId();
        SubscriptVO vo = new SubscriptVO();
        vo.setMsg("未知处理");
        vo.setUId(uid);
//        订阅业务
        //查询是否已经订阅
        LambdaQueryWrapper<EgccSubribe> query = new LambdaQueryWrapper<>();
        query.eq(EgccSubribe::getUid, uid);
        List<EgccSubribe> list = this.list(query);
        if (CollectionUtil.isEmpty(list)) {
            //添加订阅记录
            EgccSubribe subribe = new EgccSubribe();
            subribe.setState("1");
            subribe.setUid(uid);
            subribe.setCreatedTime(new Date());
            subribe.setUpdatedTime(new Date());
            this.save(subribe);

            vo.setFlag(true);
            vo.setMsg("订阅成功");
        } else {
            //获取第一条更新记录
            EgccSubribe subribe = list.get(0);
            if ("1".equals(subribe.getState())) {
                vo.setFlag(false);
                vo.setMsg("您已经订阅,请不要重复订阅");
            } else if ("0".equals(subribe.getState())) {
                subribe.setState("1");
                subribe.setUpdatedTime(new Date());
                this.updateById(subribe);

                vo.setFlag(true);
                vo.setMsg("订阅成功");
            }
        }
        return vo;
    }

    /***
     * 取消订阅
     * @return
     */
    public SubscriptVO unSubscribe(SubscriptDTO dto) {
        validateParam(dto);
        //app/unsubscribe
        String uid = dto.getUId();
        SubscriptVO vo = new SubscriptVO();
        vo.setMsg("未知处理");
        vo.setUId(uid);
//        订阅业务
        //查询是否已经订阅
        LambdaQueryWrapper<EgccSubribe> query = new LambdaQueryWrapper<>();
        query.eq(EgccSubribe::getUid, uid);
        List<EgccSubribe> list = this.list(query);
        if (CollectionUtil.isEmpty(list)) {
            vo.setFlag(false);
            vo.setMsg("您暂无订阅");
            return vo;
        } else {
            //获取第一条更新记录
            EgccSubribe subribe = list.get(0);
            if ("1".equals(subribe.getState())) {

                subribe.setState("0");
                subribe.setUpdatedTime(new Date());
                this.updateById(subribe);

                vo.setFlag(true);
                vo.setMsg("您已取消订阅");
                return vo;
            } else if ("0".equals(subribe.getState())) {
                vo.setFlag(false);
                vo.setMsg("您暂无订阅");
                return vo;
            }
        }
        return vo;
    }

    /***
     * 校验E掌通回调接口
     * sid	1	String	v50	请求流水号	需要保证在本系统中唯一
     * appId	1	String	v30	应用编号
     * time	1	String	14	请求时间	格式为：yyymmddhh24miss
     * token	1	String	v40	凭证	1.平台调用应用时为appSecret的值；
     * 2.应用调用平台时为accessToken的值；
     * 3.调用获取访问令牌接口时传固定值“accessToken”
     */
    private void validateParam(SubscriptDTO dto) {
        //校验appId
        //校验token 台调用应用时为appSecret的值；
        if (!egccConfig.getAppId().equals(dto.getAppId())) {
            log.error("校验E掌通appId失败");
            throw new CustomParameterizedException("appId不正确");
        }
        if (!egccConfig.getAppSecret().equals(dto.getToken())) {
            log.error("校验E掌通token失败");
            throw new CustomParameterizedException("token不正确");
        }
    }
}